package com.sgb.kjwl.view.ui.adapter.self.logistics;

import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.self.GoodsManageItemData;
import com.sgb.kjwl.model.entity.transport.SingleItemConstant;
import com.sgb.kjwl.utils.CargoConstant;
import com.sgb.kjwl.utils.NumberUtil;

import java.util.List;

/**
 * @author: 杨畔
 * @date: 2021/7/20 15:32
 * @Description: 货源管理列表适配器
 */
public class CargoManageItemAdapter extends BaseMultiItemQuickAdapter<GoodsManageItemData, BaseViewHolder> {
    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public CargoManageItemAdapter(List<GoodsManageItemData> data) {
        super(data);
        //普通货源管理item
        addItemType(SingleItemConstant.TYPE_ONE, R.layout.item_cargo_manage_normal);
        //竞价货源管理item
        addItemType(SingleItemConstant.TYPE_TWO, R.layout.item_cargo_manage_normal);
    }

    @Override
    protected void convert(BaseViewHolder helper, GoodsManageItemData item) {
        syncCommon(helper, item);
        switch (item.getItemType()) {
            case SingleItemConstant.TYPE_ONE:
                syncNormal(helper, item);
                break;
            case SingleItemConstant.TYPE_TWO:
                syncNormal(helper, item);
                break;
        }
    }

    /**
     * 设置公用信息
     */
    private void syncCommon(BaseViewHolder helper, GoodsManageItemData item) {
        //货物信息
        StringBuilder builder = new StringBuilder();
        builder.append(" ").append(CargoConstant.getLastGoodsTypeName(item.getGoodsTypeName(), item.getSecondGoodsTypeName()));
        if (item.getWeight() != null && item.getWeight().doubleValue() != 0) {
            builder.append("/").append(NumberUtil.trim0(item.getWeight().doubleValue())).append("吨");
        }
        if (item.getVolume() != null && item.getVolume().doubleValue() != 0) {
            builder.append("/").append(NumberUtil.trim0(item.getVolume().doubleValue())).append("方");
        }
        helper.setText(R.id.tv_item_cm_goods, builder.substring(1))
                .setText(R.id.tv_item_cm_place, String.format("%s %s ➞ %s %s",
                        item.getCityName(),
                        item.getDistrictName(),
                        item.getDestCityName(),
                        item.getDestDistrictName()))
                .setText(R.id.tv_item_cm_freight, item.getFreightType() == null || item.getFreightType() == 0
                        ? String.format("%s%s", NumberUtil.trim0(item.getFreight().floatValue()), item.getFreightUnit())
                        : "电议")
                .setText(R.id.tv_item_cm_date, CargoConstant.stampFormatListLoadTime(true,
                        item.getShipmentTime(), item.getStartPointTime(),
                        item.getShipmentEndTime(), item.getEndPointTime()));
        //设置上下架按钮
        TextView down = helper.getView(R.id.tv_item_cm_down);
        if ("0".equals(item.getStatus())) {
            //下架状态，按钮为上架
            down.setText("上架");
            down.setTextColor(ContextCompat.getColor(mContext, R.color.color_FFC000));
            down.setBackgroundResource(R.drawable.bg_cargo_manager_btn2_up);
        } else {
            //上架状态，按钮为下架
            down.setText("下架");
            down.setTextColor(ContextCompat.getColor(mContext, R.color.color_969696));
            down.setBackgroundResource(R.drawable.bg_cargo_manager_btn2);
        }
        //设置公用按钮监听
        helper.addOnClickListener(R.id.cl_item_cm)
                .addOnClickListener(R.id.tv_item_cm_edit)
                .addOnClickListener(R.id.tv_item_cm_down)
                .addOnClickListener(R.id.tv_item_cm_del);
    }

    /**
     * 同步普通货源信息
     */
    private void syncNormal(BaseViewHolder helper, GoodsManageItemData item) {
        helper.setText(R.id.tv_item_cm_msg_fee, String.format("%s元", NumberUtil.trim0(item.getMessagePrice().floatValue())))
                .setVisible(R.id.ll_item_cm_offer, item.hasOfferNum())//是否有报价单位
                .setText(R.id.tv_item_cm_offer_unit, String.format("报价车主（%s）", item.getOfferNum()))
                .setText(R.id.tv_item_cm_offer_deal, String.format("报价已处理（%s）", item.getOfferAgreed()))
                .addOnClickListener(R.id.tv_item_cm_offer_unit)
                .addOnClickListener(R.id.tv_item_cm_order);
        if (item.getPattern() != null && item.getPattern() == 1) {
            helper.setText(R.id.tv_item_goods_pattern, "竞价")
                    .setBackgroundRes(R.id.tv_item_goods_pattern, R.drawable.member_shape_gradient_1fd2ac)
                    .setVisible(R.id.tv_item_goods_pattern, true);
        } else {
            helper.setText(R.id.tv_item_goods_pattern, "普通")
                    .setBackgroundRes(R.id.tv_item_goods_pattern, R.drawable.bg_price)
                    .setVisible(R.id.tv_item_goods_pattern, false);

        }
        //普通货源状态 0 待接单  1 已成交  2 已失效  3 付款中 4 已下架
        switch (item.getState()) {
            case CargoConstant.NormalCargoMode.MODE_PENDING_ORDER:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.daijiedan)
                        .setVisible(R.id.v_item_cm_line, true)
                        .setVisible(R.id.ll_item_cm_operation, true)
                        .setVisible(R.id.tv_item_cm_edit, true)
                        .setVisible(R.id.tv_item_cm_down, true)
                        .setVisible(R.id.tv_item_cm_del, true)
                        .setVisible(R.id.tv_item_cm_order, false);
                break;
            case CargoConstant.NormalCargoMode.MODE_DEAL_DOWN:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.yichengjiao)
                        .setVisible(R.id.v_item_cm_line, true)
                        .setVisible(R.id.ll_item_cm_operation, false)
                        .setVisible(R.id.tv_item_cm_order, true);
                break;
            case CargoConstant.NormalCargoMode.MODE_EXPIRED:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.yishixiao)
                        .setVisible(R.id.v_item_cm_line, true)
                        .setVisible(R.id.ll_item_cm_operation, true)
                        .setVisible(R.id.tv_item_cm_edit, false)
                        .setVisible(R.id.tv_item_cm_down, false)
                        .setVisible(R.id.tv_item_cm_del, true)
                        .setVisible(R.id.tv_item_cm_order, false);
                break;
            case CargoConstant.NormalCargoMode.MODE_PAYING:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.ic_goods_manager_paying)
                        .setVisible(R.id.v_item_cm_line, false)
                        .setVisible(R.id.ll_item_cm_operation, false)
                        .setVisible(R.id.tv_item_cm_order, false);
                break;
            case CargoConstant.NormalCargoMode.MODE_DOWN:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.ic_goods_manager_down)
                        .setVisible(R.id.v_item_cm_line, true)
                        .setVisible(R.id.ll_item_cm_operation, true)
                        .setVisible(R.id.tv_item_cm_edit, true)
                        .setVisible(R.id.tv_item_cm_down, true)
                        .setVisible(R.id.tv_item_cm_del, true)
                        .setVisible(R.id.tv_item_cm_order, false);
                break;
        }
    }

    /**
     * 同步竞价货源信息
     */
    private void syncBidding(BaseViewHolder helper, GoodsManageItemData item) {
        //新增接单逻辑
        helper.addOnClickListener(R.id.tv_item_cm_order_unit)
                .addOnClickListener(R.id.tv_item_cm_offer_unit)
                .setVisible(R.id.ll_item_cm_order_offer, item.hasOrderNum() || item.hasOfferNum())//是否有接单或报价单位
                .setVisible(R.id.ll_item_cm_order, item.hasOrderNum())//是否有接单单位
                .setVisible(R.id.ll_item_cm_offer, item.hasOfferNum())//是否有报价单位
                .setText(R.id.tv_item_cm_order_unit, String.format("接单单位（%s）", item.getBiddingSupplyOrderNum()))
                .setText(R.id.tv_item_cm_order_deal, String.format("接单已处理（%s）", item.getTakesOrdersNum()))
                .setText(R.id.tv_item_cm_offer_unit, String.format("报价单位（%s）", item.getOfferNum()))
                .setText(R.id.tv_item_cm_offer_deal, String.format("报价已处理（%s）", item.getOfferAgreed()));
        //竞价货源状态 0 竞价中  1 已成交  2 已失效  4 报价已截止 5 已下架
        switch (item.getState()) {
            case CargoConstant.BiddingCargoMode.MODE_BIDDING:
                //报价没有同意的：可编辑、下架、删除；报价至少同意一个生成竞价订单后，货源不可编辑、下架、删除
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.jingjiazhong)
                        .setVisible(R.id.tv_item_cm_edit, !item.hasOfferOrOrderAgreed())
                        .setVisible(R.id.tv_item_cm_down, !item.hasOfferOrOrderAgreed())
                        .setVisible(R.id.tv_item_cm_del, !item.hasOfferOrOrderAgreed());
                break;
            case CargoConstant.BiddingCargoMode.MODE_DEAL_DOWN:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.yichengjiao)
                        .setVisible(R.id.tv_item_cm_edit, false)
                        .setVisible(R.id.tv_item_cm_down, false)
                        .setVisible(R.id.tv_item_cm_del, false);
                break;
            case CargoConstant.BiddingCargoMode.MODE_EXPIRED:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.yishixiao)
                        .setVisible(R.id.tv_item_cm_edit, false)
                        .setVisible(R.id.tv_item_cm_down, false)
                        .setVisible(R.id.tv_item_cm_del, true);
                break;
            case CargoConstant.BiddingCargoMode.MODE_DEADLINE:
                //报价没有同意的：可编辑、删除；报价至少同意一个生成竞价订单后，货源不可编辑、删除
                helper.setImageResource(R.id.iv_item_cm_status, R.mipmap.baojiajiezhi)
                        .setVisible(R.id.tv_item_cm_edit, !item.hasOfferOrOrderAgreed())
                        .setVisible(R.id.tv_item_cm_down, false)
                        .setVisible(R.id.tv_item_cm_del, !item.hasOfferOrOrderAgreed());
                break;
            case CargoConstant.BiddingCargoMode.MODE_DOWN:
                helper.setImageResource(R.id.iv_item_cm_status, R.drawable.ic_goods_manager_down)
                        .setVisible(R.id.tv_item_cm_edit, true)
                        .setVisible(R.id.tv_item_cm_down, true)
                        .setVisible(R.id.tv_item_cm_del, true);
                break;
        }
    }
}
